import "babel-polyfill";
import Vue from 'vue';
import VueResource from 'vue-resource'
import iView from 'iview';
import {router} from './router/index';
import addApi from '@/common/router/addApi';
import {routers, appRouter} from './router/router';
import store from './store';
import App from './app.vue';
import 'iview/dist/styles/iview.css';
import Util from '@/common/libs/util';
import Cookies from 'js-cookie';
import AliveRouterView from '@/common/components/AliveRouterView'
import iviewArea from 'iview-area';

Vue.use(iviewArea);

Vue.use(iView);
Vue.use(VueResource);
Vue.http.options.emulateJSON = true;

Vue.component('AliveRouterView', AliveRouterView)

var temp = {};
// 路由之前的操作
router.beforeEach((to, from, next) => {
	iView.LoadingBar.start();
	Util.title(to.meta.title);

	const curRouterObj = Util.getRouterObjByName(appRouter, to.name);
	console.log(to.name, appRouter);
	if (curRouterObj.permission !== undefined) { // 需要判断权限的路由
		if (Cookies.get('access') && Util.showThisRoute(curRouterObj.permission, temp.vm.$store.state.user.info.permission)) {
			next();
		} else {
			next({
				replace: true,
				name: 'error-403'
			});
		}
	} else { // 没有配置权限的路由, 直接通过
		next();
	}
});

// 路由之后的操作
router.afterEach((to) => {
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});

// 新建vue模型
temp.vm = new Vue({
    el: '#app',
    router: router,
    store: store,
	render: h => h(App),
    mounted () {
    },
    created() {
		this.$store.commit("updateRouterTree", routers);
        // 添加自定义接口函数
        addApi(this);
    }
});
